<?php
class Branch extends Controller {
    public function index() {
        $this->load->model("adminbranch_model");
        $divisionId = $this->uri->segment(4);
        $branchInfoArr = $this->adminbranch_model->getBranchByDivisionId($divisionId);
        //$populationCatRelArr = $this->adminbranch_model->getPopulationCatsRel();
        $data['branchInfoArr'] = $branchInfoArr;
        $data['divisionId'] = $divisionId;
        $this->template->load('admintemplate', 'branch/index', $data);
    }

    public function getzonesbydivs()
    {
        $outputStr = "";
        $division_id = $_POST['division_id'];
        $this->load->model("adminbranch_model");
        $result_arr = $this->adminbranch_model->getZonesByDivisionId($division_id);
        $outputStr .= "<option value=''>Select Zone</option>";
        foreach ($result_arr as $result)
        {
            $outputStr .= "<option value='" . $result['zoneid'] . "'>" . $result['zonename'] . "</option>";
        }

        echo $outputStr;
    }

    public function addbranch()
    {
        $this->load->model("adminbranch_model");
        $data['populationcat_arr'] = $this->adminbranch_model->getPopulationCats();
        $this->template->load('admintemplate', 'branch/addbranch', $data);        
    }

    public function doaddbranch()
    {
        $this->load->model("adminbranch_model");
        $add_branch = $this->adminbranch_model->addBranch($_POST);
        if($add_branch)
        {
            //$this->template->load('admintemplate', 'secu/branch/index');
            redirect('secu/branch/index/' . $_POST['division'], 'refresh');
        } else {
            $data['error_msg'] = "Error has occured while adding new branch. Please contact system administrator";
            $this->template->load('admintemplate', 'secu/branch/addbranch', $data);
        }
    }

    public function getbranchbydivision()
    {
        $outputStr = "";
        $division_id = $_POST['division_id'];
        $this->load->model("adminbranch_model");
        $result_arr = $this->adminbranch_model->getBranchByDivisionId($division_id);
        
        $outputStr .= "<option value=''>Select Branch</option>";
        foreach ($result_arr as $result)
        {
            $outputStr .= "<option value='" . $result['branchid'] . "'>" . $result['branchname'] . "</option>";
        }

        echo $outputStr;
    }

    public function getorgbybranch()
    {
        $outputStr = "";
        $division_id = $_POST['division_id'];
        $branch_id = $_POST['branch_id'];
        
        $this->load->model("adminbranch_model");
        $result_arr = $this->adminbranch_model->getOrgByBranchId($division_id, $branch_id);

        $outputStr .= "<option value=''>Select Branch</option>";
        foreach ($result_arr as $result)
        {
            $outputStr .= "<option value='" . $result['orgid'] . "'>" . $result['orgname'] . "</option>";
        }

        echo $outputStr;
    }

    public function editbranch()
    {
        $branch_id = $this->uri->segment(4);
        $this->load->model('adminbranch_model');
        $branch_arr = $this->adminbranch_model->getBranchDetailsById($branch_id);
        $data['populationcat_arr'] = $this->adminbranch_model->getPopulationCats();
        $population_arr = $this->adminbranch_model->getPopulationCatByBranchId($branch_id);
        $data['branch_arr'] = $branch_arr;
        $data['population_arr'] = $population_arr;
        $this->template->load('admintemplate', 'secu/branch/editbranch', $data);
    }

    public function doeditbranch()
    {
        $this->load->model("adminbranch_model");
        $add_branch = $this->adminbranch_model->updateBranch($_POST);
        if($add_branch)
        {
            //$this->template->load('admintemplate', 'secu/branch/index');
            redirect('secu/branch/index/' . $_POST['division'], 'refresh');
        } else {
            $data['error_msg'] = "Error has occured while updating branch. Please contact system administrator";
            $this->template->load('admintemplate', 'secu/branch/addbranch', $data);
        }
    }

    public function showbranchmore()
    {
        $return_str = "";
        $total = 0;
        $branch_id = $_POST['branch_id'];
        $category = $_POST['category'];
        $this->load->model("adminbranch_model");
        $populationCatRelArr = $this->adminbranch_model->getPopulationCatsRelByBranchId($branch_id, $category);


        $return_str .= "<table class='tablesorter' border='0' cellspacing='0' style='width:50%;'>";
        $return_str .= "<thead>";
        $return_str .= "<tr>";
        //$return_str .= "<td align='center'>Branch Id</td>";
        $return_str .= "<td align='center'>&nbsp;</td>";
        $return_str .= "<td align='center'><b>Population Count</b></td>";
        $return_str .= "</tr>";
        $return_str .= "</thead>";
        $return_str .= "<tbody>";

        foreach ($populationCatRelArr as $populationCatInfo) :
            $total = $total + $populationCatInfo['populationcount'];

        $return_str .= "<tr>";
            //$return_str .= "<td align='center'>" . $populationCatInfo['tbl_branches_branchid'] . "</td>";

            switch ($populationCatInfo['populationcatdesc'])
            {
                case "buddhist" :
                    $symbol = "buddhist.png";
                    break;

                case "catholic" :
                    $symbol = "christian.png";
                    break;

                case "islam" :
                    $symbol = "islam.png";
                    break;

                case "hindu" :
                    $symbol = "hindu.jpg";
                    break;

                case "other_rel" :
                    $symbol = "";
                    break;
            }


            $return_str .= "<td align='center'>";
            if(!empty($symbol)) {
                $return_str .= "<img src='" . base_url() . "images/symbols/" . $symbol . "' width='30' height='30' />";
            } else {
                if($populationCatInfo['populationcatdesc'] == "other_rel") {
                    $return_str .= ucfirst(str_replace('other_rel', 'Other', $populationCatInfo['populationcatdesc']));
                } else if($populationCatInfo['populationcatdesc'] == "other_nat") {
                    $return_str .= ucfirst(str_replace('other_nat', 'Other', $populationCatInfo['populationcatdesc']));
                } else {
                    $return_str1 = ucfirst(str_replace('_', '-', $populationCatInfo['populationcatdesc']));
                    $return_str .= ucfirst(str_replace('Age-', 'Age ', $return_str1));
                }
            }
            $return_str .= "</td>";
            $return_str .= "<td align='center'>" . $populationCatInfo['populationcount'] . "</td>";
            $return_str .= "</tr>";

        endforeach;

        $return_str .= "<tr><td align='center'><b>Total</b></td><td align='center'><div class='total'>" . $total . "</div></td>";

        $return_str .= "</tbody>";
        $return_str .= "</table>";

        echo $return_str;
    }

    public function delbranch()
    {
        $branchId = $_POST['branchid'];
        $this->load->model("adminbranch_model");

        $delBranchDetails = $this->adminbranch_model->deleteBranchDetails($branchId);
    }
}
?>
